/*
 *
 * Copyright(c) 2010 Safelayer Secure Communications
 *
 * This is open source software, placed under the terms of the
 * GNU GENERAL PUBLIC LICENSE Version 3.
 * Please see: http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Permission to use, copy, modify, and distribute this software
 * for any purpose and without fee is hereby granted, provided
 * that the above copyright notices appear in all copies and that
 * both the copyright notice and this permission notice appear in
 * supporting documentation.
 * 
 */
package com.neuralnoise.foaf.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
 * Bridge class which can be used to read RDF graphs from severals sources (files, urls and
 * inputStreams) to provide as a result the utility class {@link FOAF} to perform operations over
 * an RDF graph.
 * 
 * @author victor.martinez <swtp@safelayer.com>
 */
public class FOAFReader {

	private Model model;

	private FOAFReader(File file, Model model, String base) throws FileNotFoundException {
		this.model = model.read(new FileInputStream(file), base);
	}

	private FOAFReader(String url, Model model) {
		this.model = model.read(url);
	}

	private FOAFReader(InputStream in, Model model, String base) {
		this.model = model.read(in, base);
	}

	/* static factory methods */

	/**
	 * Returns an instance of this class from a <code>file</code>.
	 * 
	 * @param file The file to read.
	 * @param base The base uri to be used when converting relative URI's to absolute URI's. If
	 *            there are no relative URIs in the source, this argument may safely be null. If the
	 *            base is the empty string, then relative URIs
	 *            <code>will be retained in the model</code>.
	 * 
	 * @return an instance of this class from a <code>url</code>.
	 */
	public static FOAFReader newInstanceFromFile(File file, String base) throws FileNotFoundException {
		return new FOAFReader(file, ModelFactory.createDefaultModel(), base);
	}

	/**
	 * Returns an instance of this class from a <code>url</code>.
	 * 
	 * @param url Read serialized RDF from a <code>url</code>.
	 * 
	 * @return an instance of this class from a <code>url</code>.
	 */
	public static FOAFReader newInstanceFromUrl(String url) {
		return new FOAFReader(url, ModelFactory.createDefaultModel());
	}

	/**
	 * Returns an instance of this class from a <code>InputStream</code>.
	 * 
	 * @param in The InputStream from which to read
	 * @param base The base uri to be used when converting relative URI's to absolute URI's. If
	 *            there are no relative URIs in the source, this argument may safely be null. If the
	 *            base is the empty string, then relative URIs
	 *            <code>will be retained in the model</code>.
	 * @return an instance of this class from a <code>InputStream</code>.
	 */
	public static FOAFReader newInstanceFromStream(InputStream in, String base) {
		return new FOAFReader(in, ModelFactory.createDefaultModel(), base);
	}

	/**
	 * Returns an utility class {@link FOAF} to perform operations over the RDF graph previously
	 * read.
	 * 
	 * @return An utility class {@link FOAF} to perform operations over the RDF graph previously
	 *         read
	 */
	public FOAF read() {
		return new FOAF(model);
	}

}
